<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			body {
				background: #EFEFEF;
			}
			
			header {
				height: 30px;
				padding: 12px 24px;
				background: #333333;
				color: #FFFFFF;
			}
			.fl {
				float: left;
			}
			.fr {
				float: right;
			}
			.mg-top-10 {
				margin-top: 10px;
			}
			h3 {
				font-size: 28px;
			}
			.main {
				margin: 10px auto;
				width: 70%;
			}
			.list {
				width: 337px;
				background: #44ACFF;
				padding-left: 20px;
				color: #FFFFFF;
			}
			
			.list li {
				font-size: 22px;
				line-height: 2;
				padding-left: 20px;
				cursor: pointer;
			}
			
			.list li:hover {
				background: #333333;
				border-left: 2px solid #FFFFFF;
			}
			
			ul {
				list-style: none;
			}
			
			.content {
				background: #FFFFFF;
				margin-left: 367px;
			}
			
			img {
				height: 400px;
				width: 100%;
			}
			
			.active {
				background: #333333;
				border-left: 2px solid #FFFFFF;
			}
		</style>
	</head>

	<body>
		<header>H5 history api history </header>
		<div class="main mg-top-10">
			<aside class="list fl">
				<h3>NAV</h3>
				<ul>
					<li class="active" data-type="girl">美女</li>
					<li data-type="car">香车</li>
					<li data-type="computer">电脑</li>
					<li data-type="since">风景</li>
				</ul>
			</aside>
			<div class="content">
				<img src="img/girl.jpg" />
			</div>
		</div>
	</body>

</html>

<script type="text/javascript">
	// 解决不能访问首页的问题
	window.history.replaceState({type:'girl'},'','#girl')
	
	var list = document.querySelector('ul');
	// 显示图片标签
	var img = document.querySelector('img');
	list.onclick = function (ev) {
		var target = ev.target;
		var type = target.dataset.type;
		var lis = list.children;
		// 如果点击的不是li，返回
		if (target.nodeName !== 'LI') {
			return false
		};
		// 移除其他标签的active类
		for (var i = 0; i < lis.length; i++) {
			lis[i].classList.remove('active');
		}
		// 给当前点击的li加上active类
		target.classList.add('active');
		img.src = `img/${type}.jpg`;
		var url = `#${type}`;
		window.history.pushState({ type: type }, '', url)
	};

	window.onpopstate = function (e) {
		var type = e.state && e.state.type;
		console.log(type);
		if (type) {
			document.querySelector(`.active`).className = '';
			document.querySelector(`[data-type=${type}]`).className = 'active';
			img.src = `img/${type}.jpg`;
		}
	}
</script>